using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class ReduceDAL
    {
        public static int Save(ReduceInfo info)
        {
            string sql = string.Format("insert into [reduce]([ActiveID],[HourCount],[State],[LastEditTime],[CreateDate]) values(@ActiveID,@HourCount,@State,@LastEditTime,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ActiveID", info.ActiveID),
                new SqlParameter("@HourCount", info.HourCount),
                new SqlParameter("@State", info.State),
                new SqlParameter("@LastEditTime", info.LastEditTime),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar2(sql, paras));
        }
        public static int Update(ReduceInfo info)
        {
            string sql = string.Format("update [reduce] set [State]=@State,[HourCount]=@HourCount,[LastEditTime]=getdate() where ID=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@State", info.State),
                new SqlParameter("@HourCount", info.HourCount),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static int PauseReduce(int activeid, int state)
        {
            string sql = string.Format("update [reduce] set [State]=@State,[LastEditTime]=@LastEditTime where [ActiveID]=@ActiveID and State=1");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@State", state),
                new SqlParameter("@LastEditDate", DateTime.Now),
                new SqlParameter("@ActiveID", activeid)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? activeid : 0;
        }
        public static int StartReduce(int activeid, int state)
        {
            string sql = string.Format("update [reduce] set [State]=@State,[LastEditTime]=@LastEditTime where [ActiveID]=@ActiveID and State=2");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@State", state),
                new SqlParameter("@LastEditDate", DateTime.Now),
                new SqlParameter("@ActiveID", activeid)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? activeid : 0;
        }
        public static ReduceInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[reduce]");
            return Model(dr, null);
        }
        public static int FinishReduce(int id)
        {
            string sql = string.Format("update [reduce] set [State]=3 where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ID", id)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? id : 0;
        }
        public static int DecReduce(int id)
        {
            string sql = string.Format("update [reduce] set LastEditTime=getdate() where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ID", id)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? id : 0;
        }
        public static bool HasRun(int activeid)
        {
            int count = DataConvert.ToInt(CommonDAL.ExecuteScalar(string.Format("select count(1) from [reduce] where ActiveID={0} and State=1",activeid)));
            return count > 0 ? true : false;
        }
        public static List<ReduceInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[reduce]");
            return ListModel(dt);
        }
        public static List<ReduceInfo> FindPage(string keyword, int state, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (state!=-1)
            {
                filter.AppendFormat(" and [reduce].State = {0}", state);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [active].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [reduce].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindCPage(startindex, pagesize, order, desc, filter.ToString(), "[reduce_page]", out count);
            return ListModel(dt);
        }

        public static List<ReduceInfo> FindListOfRun()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [reduce] where State=1"));
            return ListModel(dt);
        }
        private static ReduceInfo Model(DataRow dr, DataColumnCollection col)
        {
            ReduceInfo info = new ReduceInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.ActiveID = DataConvert.ToInt(dr["ActiveID"]);
                info.HourCount = DataConvert.ToInt(dr["HourCount"]);
                info.State = DataConvert.ToInt(dr["State"]);
                info.LastEditTime = DataConvert.ToDateTime(dr["LastEditTime"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);

                info.StateText = Commons.ReduceState[info.State];
                if (col !=null && col.Contains("Name"))
                {
                    info.Name = DataConvert.ToString(dr["Name"]);
                }

            }
            return info;
        }
        private static ReduceInfo Model(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                return Model(dt.Rows[0], dt.Columns);
            }
            else
            {
                return new ReduceInfo();
            }
        }
        private static List<ReduceInfo> ListModel(DataTable dt)
        {
            List<ReduceInfo> list = new List<ReduceInfo>();
            DataColumnCollection col = dt.Columns;
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Model(row, col));
            }
            return list;
        }
    }
}

